package com.sgcc.pda.lib_tools_database.util;

import android.text.TextUtils;

import com.sgcc.pda.lib_tools_database.comment.DBConstant;
import com.sgcc.pda.lib_tools_database.gen.DaoMaster;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import static com.sgcc.pda.lib_tools_database.comment.DBConstant.DEFAULT_DB_NAME;
import static com.sgcc.pda.lib_tools_database.comment.DBConstant.DEFAULT_USER_ID;

public class DataBaseConfigTools {

    /**
     * 本地数据库是否存在
     */
    public static boolean isDBExist() {
        try {
            File file = new File(
                    DBConstant.DB_PATH + DEFAULT_USER_ID + "/" + DEFAULT_DB_NAME);

            if (!file.exists()) return false;
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    /**
     * 创建文件夹及文件
     */
    public static void createText(String userId, String version) throws IOException {
        if (TextUtils.isEmpty(version)) {
            version = DaoMaster.SCHEMA_VERSION + "";
        }
        File file = new File(DBConstant.DB_PATH + userId + "/");
        if (!file.exists()) {
            try {
                //按照指定的路径创建文件夹
                file.mkdirs();
            } catch (Exception e) {

            }
        }
        File dir = new File(file.getAbsolutePath() + "/data.txt");
        if (dir.exists()) {
            dir.delete();
        }
        if (!dir.exists()) {
            try {
                //在指定的文件夹中创建文件
                dir.createNewFile();
                print(dir.getAbsolutePath(), version);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 向已创建的文件中写入数据
     */
    private static void print(String path, String str) {
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(path, true);
            bw = new BufferedWriter(fw);
            bw.write(str);
            bw.newLine();
            bw.flush(); // 刷新该流的缓冲
            bw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
            try {
                bw.close();
                fw.close();
            } catch (IOException e1) {

            }
        }
    }

    /**
     * 从app应用包路径下读取文件中信息
     */
    public static String readInfo(String userId) {
        File file = new File(DBConstant.DB_PATH + userId + "/dataBaseConfig.txt");
        if (!file.exists()) {
            return DaoMaster.SCHEMA_VERSION + "";
        }
        try {
            FileInputStream inputStream = new FileInputStream(file);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String info = bufferedReader.readLine();
            return info;
        } catch (Exception e) {
            e.printStackTrace();
            return DaoMaster.SCHEMA_VERSION + "";
        }
    }

}
